package com.ruikong.dazhi.service.impl.baseinfo;

import com.ruikong.dazhi.dao.basicinfo.DeviceMapper;
import com.ruikong.dazhi.entity.basicinfo.DeviceType;
import com.ruikong.dazhi.dao.basicinfo.DeviceTypeMapper;
import com.ruikong.dazhi.entity.basicinfo.DeviceType;
import com.ruikong.dazhi.model.output.TreeMenuOutput;
import com.ruikong.dazhi.service.basicinfo.IDeviceTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * (DeviceType)表服务实现类
 *
 * @author HuaJiaYi
 * @since 2021-09-29 10:55:42
 */
@Service("deviceTypeService")
public class DeviceTypeServiceImpl implements IDeviceTypeService {
    @Autowired
    private DeviceTypeMapper deviceTypeMapper;
    @Autowired
    private DeviceMapper deviceMapper;


    @Override
    public DeviceType queryById(Integer deviceTypeId) {
        return this.deviceTypeMapper.queryById(deviceTypeId);
    }



    @Override
    public int insert(DeviceType deviceType) {
        if (deviceTypeMapper.getId() == null) {
            deviceType.setDeviceTypeId(1);
        } else {
            deviceType.setDeviceTypeId(deviceTypeMapper.getId() + 1);
        }
        return deviceTypeMapper.insert(deviceType);
    }

    @Override
    public int update(DeviceType deviceType) {
        return deviceTypeMapper.update(deviceType);
    }

    @Override
    public String deleteById(Integer deviceTypeId) {
        //这个类下面是否有子类
        List<DeviceType> list = queryByPid(deviceTypeId);
        if (list.size() > 0) {
            return "要删除的设备类别存在子类别，不允许被删除";
        }
        int iCount = deviceMapper.queryByTypeId(deviceTypeId);
        if (iCount > 0) {
            return "要删除的设备类别存在设备，不允许被删除";
        }
        if (deviceTypeMapper.deleteById(deviceTypeId) > 0) {
            return "";
        } else {
            return "删除失败";
        }
    }

    public void addTree(List<TreeMenuOutput> tree, int deviceTypePid) {
        List<DeviceType> DeviceType_list = deviceTypeMapper.queryByPid(deviceTypePid);
        for (DeviceType root : DeviceType_list) {
            TreeMenuOutput menu = new TreeMenuOutput();
            menu.setId(root.getDeviceTypeId());
            menu.setName(root.getDeviceTypeName());
            menu.setpId(deviceTypePid);
            tree.add(menu);
            addTree(tree, menu.getId());
        }
    }

    @Override
    public List<TreeMenuOutput> getDeviceTypeTree() {
        List<TreeMenuOutput> tree_list = new ArrayList<>();
        TreeMenuOutput root = new TreeMenuOutput();
        root.setpId(-1);
        root.setId(0);
        root.setName("设备类别");
        tree_list.add(root);
        this.addTree(tree_list, 0);
        return tree_list;
    }


    @Override
    public Integer isExist(DeviceType s) {
        return this.deviceTypeMapper.isExist(s);
    }

    @Override
    public List<DeviceType> queryByPid(int deviceTypePid) {
        return this.deviceTypeMapper.queryByPid(deviceTypePid);
    }


}
